package com.atguigu.exercise1;

import java.util.concurrent.TimeUnit;

class Ticket {
    public Integer num = 200;

    public synchronized void sale() {
        try {
            System.out.println(Thread.currentThread().getName() + "\t" + "窗口正在出售!");
            num--;
            TimeUnit.SECONDS.sleep(1);
            System.out.println(Thread.currentThread().getName() + "\t" + "窗口售票已完成,票数剩余:" + num);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
}

public class TicketSold {

    public static void main(String[] args) {
        Ticket ticket = new Ticket();

        new Thread(()->{
            for (int i = 0; i < 100; i++) {
                ticket.sale();
            }
        }).start();

        new Thread(()->{
            for (int i = 0; i < 100; i++) {
                ticket.sale();
            }
        },"B").start();

        new Thread(()->{
            for (int i = 0; i < 100; i++) {
                ticket.sale();
            }
        }, "C").start();
    }
}
